package com.sdry.web.controller.jyy;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.jyy.Receive;
import com.sdry.model.jyy.ReceiveDetail;
import com.sdry.model.jyy.Up;
import com.sdry.model.llm.LlmWarehouseStock;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.Materiel;
import com.sdry.model.tdd.Unpacking;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.service.jyy.ReceiveDetailService;
import com.sdry.service.jyy.ReceiveService;
import com.sdry.service.jyy.UpService;
import com.sdry.service.llm.InquiryBusinessService;
import com.sdry.service.lz.MaterielService;
import com.sdry.service.tdd.OthersService;
import com.sdry.service.tdd.UnpackingService;
import com.sdry.service.zc.ZcInventoryManagementService;
import com.sdry.utils.DateUtil;
import com.sdry.utils.PushExample;
import com.sdry.utils.ResponseUtil;

/**
 * 收货和入库的app接口
 * @author jyy
 * @2019-05-16 14:15:38
 */
@RequestMapping("/app")
@Controller
public class AppJyController {
	@Resource ReceiveService ReceiveService;
	@Resource ReceiveDetailService ReceiveDetailService;
	@Resource InquiryBusinessService inquiryBusinessService;
	@Resource UpService UpService;
	@Resource ZcInventoryManagementService zcInventoryManagementService;
	@Resource UnpackingService unpackingService;
	@Resource MaterielService materielService;
	/**
	 * 给app查询所有未收货的收货单
	 * @param response 
	 * @throws Exception
	 */
	@RequestMapping("/selectAllReceive")
	public void selectAllReceive(HttpServletResponse response) throws Exception {
		Receive param = new Receive();
		//未收货
		param.setState(0);
		List<Receive> list = ReceiveService.queryAllByMution(param);
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据收货单号查询收货详情
	 * @param receiveNumber 收货单号
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/selectReceiveDtailByNumber")
	public void selectReceiveDtailByNumber(String receiveNumber,HttpServletResponse response) throws Exception {
		
		ReceiveDetail param = new ReceiveDetail();
		param.setReceiveNumber(receiveNumber);
		List<ReceiveDetail> list = ReceiveDetailService.queryAllByMution(param );
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 收货提交
	 * @param Details 收货详情集合
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/submitReceive")
	public void submitReceive(String Details, String person,  HttpServletResponse response) throws Exception{
		
		//初始化影响行数
		int affact = 0;
		
		//json参数转化为实体集合
		Gson gson = new Gson();
		List<ReceiveDetail> list= gson.fromJson(Details,new TypeToken<List<ReceiveDetail>>() {}.getType());
		
		//遍历修改
		for (ReceiveDetail receiveDetail : list) {
			affact = ReceiveDetailService.update(receiveDetail);
		}
		
		//获取收货单号
		ReceiveDetail receiveDetail = list.get(0);
		//更改一级状态  --为已收货
		Receive receive = new Receive();
		receive.setReceiveNumber(receiveDetail.getReceiveNumber());
		List<Receive> receives = ReceiveService.queryAllByMution(receive);
		
		if(receives.size() >0) {
			Receive receive2 = receives.get(0);
			receive2.setState(1);
			
			receive2.setReceiveDate(DateUtil.dateFormat1());
			receive2.setReceiveName(person);
			ReceiveService.update(receive2);
		}
		
		JSONObject jsonObject = new JSONObject();
		if (affact>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", "提交成功");
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "提交失败");
		    jsonObject.put("data", "0");
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据物料产品码查询物料所有信息
	 * @param maNum 物料产品码
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/selectMaByMaNum")
	public void selectMaByMaNum(String maNum, HttpServletResponse response) throws Exception {
		Materiel materiel = ReceiveService.selectMaByMaNum(maNum);
		if(materiel != null) {
			if(materiel.getId() != null) {
				List<Customer> customers = ReceiveService.selectCustomerByMid(materiel.getId());
				
				if(customers.size() > 0) {
					materiel.setCustomer(customers);
				}
			}
		}
		
		JSONObject jsonObject = new JSONObject();
		if (materiel != null) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", materiel);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据不存在");
		    jsonObject.put("data", materiel);
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据物料id查询，存放该物料的仓库、库区、库位信息
	 * @param mid 物料id
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/selectWarehouseByMid")
	public void selectWarehouseByMid(String mid, String customer, HttpServletResponse response) throws Exception {
		
		Up up = new Up();
		//物料id
		if(mid != null && mid != "") {
			up.setMid(Long.valueOf(mid));
		}
		//供应商id
		if(customer != null) {
			up.setCustomerId(Long.valueOf(customer));
		}
		List<LlmWarehouseStock> list = new ArrayList<LlmWarehouseStock>();
		
		if(mid != null && mid != "") {
			list = ReceiveService.selectWarehouseByMid(up);
		}else {
			list = inquiryBusinessService.appGetStock();
		}
		
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		ResponseUtil.write(response, jsonObject);
		
	}
	
	
	/**
	 * 入库
	 * @param stocks 入库详情集合
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/comeku")
	public void comeku(String json, HttpServletResponse response) throws Exception {
		
		Long affact = 0L;
		
		 JSONObject jsonObject2 = JSONObject.fromObject(json);
		 Up up=(Up)JSONObject.toBean(jsonObject2, Up.class);
			
			//通过仓库和库区名称查询库区id
			Long srcRegionId = zcInventoryManagementService.selectSrcRegionIdByName(up.getCqname(),up.getKqname());
			up.setKq(srcRegionId);
			if(null != up.getKwname() && !"".equals(up.getKwname())){
				//通过库区和库位名称查询库位id
				Long srcLocationId = zcInventoryManagementService.selectSrcLocationIdByName(up.getKqname(),up.getKwname());
				up.setKw(srcLocationId);
			}
			
			//通过仓库名称查仓库id
			Long srcwarehouseid = UpService.getwidByWname(up.getCqname());
			up.setCq(srcwarehouseid);
			
			//1. 添加入库记录
			up.setCreateDate(DateUtil.dateFormat1());
			affact = UpService.insert(up);
			
			//2.增加库存信息
				//根据mid查询有无此信息
			ZcInventoryInfoEntity zcInventoryInfoEntity = UpService.selectByMid(up);
			if(zcInventoryInfoEntity != null) {
				int mNum = zcInventoryInfoEntity.getmNum();
				mNum = up.getNumber() + mNum;
				zcInventoryInfoEntity.setmNum(mNum);
				affact = UpService.editStock(zcInventoryInfoEntity);
				
				int floor = zcInventoryInfoEntity.getFloor();
				int upper = zcInventoryInfoEntity.getUpper();
				//根据物料id查询物料信息
				Materiel materiel = materielService.queryMaterielById(up.getMid());
				String materiel_num = materiel.getMateriel_num();
				String materiel_name = materiel.getMateriel_name();
				
				//如果库存大于或小于预警值  报警
				String content = "";
				String people = "";
				if(mNum > upper) {
					content = materiel_num+"-"+materiel_name+"-"+"库存值大于最大预警值";
					 PushExample.testSendPush(content , "库存报警", people, "0");
				}else if(mNum < floor) {
					content = materiel_num+"-"+materiel_name+"-"+"库存值小于最小预警值";
					PushExample.testSendPush(content , "库存报警", people, "0");
				}
			}else {
				affact = UpService.insertStock(up);
			}
			
			//3.生成拆箱任务
			Unpacking param = new Unpacking();
			param.setMid(up.getMid());
			param.setCustomer_id(up.getCustomerId());
				//根据mid查询有无此信息
				List<Unpacking> list = unpackingService.queryAllByMution(param );
				
				if(list.size() > 0) {
					//有：数量累加
					Unpacking unpacking = list.get(0);
					int unum = unpacking.getUnum();
					unpacking.setUnum(unum + up.getNumber());
					unpackingService.update(unpacking);
				}else {
					//没有：新增
					param.setUnum(up.getNumber());
					unpackingService.insert(param);
				}
			
			//4. 判断是否新增了库位
			if(up.getRemark() != null && up.getRemark() != "") {
				if("1".equals(up.getRemark())) {
					//是：新增库位
					UpService.insertMw(up);
				}
			}
			//5.新增报警信息(预留)
			//TODO
		
		
		JSONObject jsonObject = new JSONObject();
		if (affact>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", "提交成功");
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "提交失败");
		    jsonObject.put("data", "0");
		}
		ResponseUtil.write(response, jsonObject);
		
	}

}
